//
//  FKTabController.swift
//  FireKnowledge
//
//  Created by 杨伟毅 on 2019/4/19.
//  Copyright © 2019 wely. All rights reserved.
//

import UIKit

class FKTabController: UITabBarController {
 var indexFlag = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initItemAttrs()
        addchildVC()
        // Do any additional setup after loading the view.
    }
    func initItemAttrs() {
        let item : UITabBarItem = UITabBarItem.appearance()
        item.setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10),NSAttributedString.Key.foregroundColor : FKGrobalColor.FKNoSelectColor], for: .normal)
        item.setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10),NSAttributedString.Key.foregroundColor : FKGrobalColor.FKSelectColor], for: .selected)
        self.tabBar.isTranslucent = false
    }
    func addchildVC()
    {
        setUpChilderVC(baseVC: FKFoundBaseVC(), tabTitle: "发现", navTitle: "", image:"faxian", selectedImage: "faxianSeleted")
         setUpChilderVC(baseVC: FKBaseCourseVC(), tabTitle: "课程", navTitle: "", image:"faxian", selectedImage: "faxianSeleted")
         setUpChilderVC(baseVC: FKPersonalBaseVC(), tabTitle: "个人中心", navTitle: "", image:"faxian", selectedImage: "faxianSeleted")
    }
    func setUpChilderVC(baseVC:UIViewController,tabTitle:String,navTitle:String,image:String,selectedImage:String)
    {
        let baseNavVC = FkNavViewController.init(rootViewController:baseVC)
        self.addChild(baseNavVC)
        baseNavVC.tabBarItem.title = tabTitle
        baseVC.navigationItem.title = navTitle
        baseNavVC.tabBarItem.image = UIImage.init(named: image)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        baseNavVC.tabBarItem.selectedImage = UIImage.init(named: selectedImage)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
    }
    
    override var shouldAutorotate: Bool{
        return (self.viewControllers?.first!.shouldAutorotate)!
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension FKTabController:UITabBarControllerDelegate {
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
//        let selectIndex = self.tabBar.items?.index(of: item)
      let selectIndex = self.tabBar.items?.firstIndex(of: item)
        //声明的私有属性
        if self.indexFlag != selectIndex{
            self.tabbarItemAnimationWithIndex(index: selectIndex!)
        }
    }
    func tabbarItemAnimationWithIndex(index: Int){
        var tabbarItem : [UIView] = []
        for tabBarButton in self.tabBar.subviews{
            if tabBarButton.isKind(of: NSClassFromString("UITabBarButton")!){
                tabbarItem.append(tabBarButton)
            }
        }
        let pulse = CABasicAnimation.init(keyPath: "transform.scale")
        pulse.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeInEaseOut)
        pulse.duration = 0.1
        pulse.repeatCount = 1
        pulse.fromValue = NSNumber.init(value: 0.8)
        pulse.toValue = NSNumber.init(value: 1.0)
        let itemView = tabbarItem[index]
        itemView.layer.add(pulse, forKey: "")
        self.indexFlag = index//声明的私有属性
    }
}
